<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="关键字" prop="keywords">
          <el-input v-model="queryParams.keywords" placeholder="用户名/昵称/手机号" clearable style="width: 200px"
            @keyup.enter="handleQuery" />
        </el-form-item>

        <el-form-item label="客户状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="全部" clearable class="!w-[200px]">
            <el-option label="全部" :value="0" />
            <el-option label="代付款" :value="1" />
            <el-option label="已付款" :value="2" />
            <el-option label="已退款" :value="3" />
          </el-select>
        </el-form-item>

        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker :editable="false" class="!w-[240px]" v-model="queryParams.createTime" type="daterange"
            range-separator="~" value-format="x" start-placeholder="开始时间" end-placeholder="截止时间" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table v-loading="loading" :data="pageData" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="序号" align="center" type="index" width="80" />
        <el-table-column label="用户姓名" align="center" prop="user_name" width="150" />
        <el-table-column label="用户手机号" align="center" prop="user_tel" width="150" />
        <el-table-column label="金额" align="center" prop="price" />
        <el-table-column label="充值类型" align="center" width="150">
          <template #default="{ row }">
            <span v-if="row.stage == 0">充值余额</span>
            <span v-if="row.stage == 1">充值会员</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" width="150">
          <template #default="{ row }">
            <span v-if="row.handled == 0">待付款</span>
            <span v-if="row.handled == 1">已付款</span>
            <span v-if="row.handled == -1">已退款</span>
          </template>
        </el-table-column>
        <el-table-column label="支付时间" align="center" width="180">
          <template #default="scope">
            <template v-if="scope.row.pay_time > 0">
              {{ formatTimestamp(scope.row.pay_time * 1000) }}
            </template>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" width="180">
          <template #default="scope">
            <template v-if="scope.row.created_at > 0">
              {{ formatTimestamp(scope.row.created_at * 1000) }}
            </template>
          </template>
        </el-table-column>
        <el-table-column label="退款操作人" align="center" prop="admin_name" width="150" />
        <el-table-column label="操作" fixed="right" width="80">
          <template #default="scope">
            <el-button v-if="scope.row.handled == 1" v-hasPerm="['pay/refund']" type="primary" link size="small"
              @click="handleRemarks(scope.row)">
              <i-ep-refresh />
              退款
            </el-button>
            <!-- <el-button type="primary" link size="small" @click="handleInfo(scope.row.id)">
              <i-ep-document />
              详情
            </el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "User",
  inheritAttrs: false,
});

import TradeAPI, { UserPageQuery, UserPageVO } from "@/api/tradeLog";
import { formatTimestamp } from '@/utils/index'

const queryFormRef = ref(ElForm);

const loading = ref(false);
const removeIds = ref([]);
const total = ref(0);
const pageData = ref<UserPageVO[]>();
/** 用户查询参数  */
const queryParams = reactive<UserPageQuery>({
  pageNum: 1,
  pageSize: 10,
  createTime: []
});

// 跳转详情
const handleInfo = (row: any) => {
  console.log(row);
}

// 退款
const handleRemarks = (row: { [key: string]: any }) => {
  ElMessageBox.confirm(
    '确定退款吗?',
    '谨慎操作',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      TradeAPI.getPayRefund({ id: row.id }).then((res: any) => {
        ElMessage({
          type: 'success',
          message: res.msg,
        })
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '点击取消',
      })
    })

}

/** 查询 */
function handleQuery() {
  loading.value = true;
  if (queryParams.createTime.length > 0) {
    queryParams.startDate = queryParams.createTime[0] / 1000;
    queryParams.endDate = queryParams.createTime[1] / 1000;
  }
  TradeAPI.getPayPage(queryParams)
    .then((data) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  queryParams.startDate = 0;
  queryParams.endDate = 0;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

onMounted(() => {
  handleQuery();
});
</script>
